//
//  JYVisitorView.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/9.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol JYVisitorDelegate : NSObjectProtocol {

    // 2.定义代理方法
    func visitorViewDidClickRegisterButton()
    
    func visitorViewDidClickLoginButton()
}


class JYVisitorView: UIView {

    //3.定义代理属性
    weak var delegate: JYVisitorDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //JYVisitorView() 这个方法就相当于 alloca init ...
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    //核心动画让转盘转起来
    func startAnimation () {
        //使用什么类型的动画
    let anima = CABasicAnimation(keyPath: "transform.rotation")
        //动画要变化的值
    anima.toValue = M_PI * 2
        //重复次数
    anima.repeatCount = MAXFLOAT
        //完成一个动画时间
    anima.duration = 20
        
        //跳转到其他界面时间,系统认为动画已经完成,再次回到本界面时,就不动了,所有要不要让动画移出
    anima.removedOnCompletion = false
        
        //添加到图层上面去
    iconView.layer.addAnimation(anima, forKey: nil)
        
    }
    
    //Mark -公开方法
    func setVisitorView (imageName: String , message: String) {
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        houseView.hidden = true
        coverView.hidden = true
    }
    
    
    private func prepareUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 错误说明: 代码约束时,系统会自动帮我们添加约束,所以,需要禁用系统的,才不会对我们的造成影响
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        // 2.添加约束
        // 约束都往父控件添加
        /// item: 要约束的view
        /// attribute: 要约束的view的什么属性
        /// relatedBy: 参照关系
        /// toItem: 要参照的view
        /// attribute: 参照的view的什么属性
        
        // 将约束添加到父控件身上 (用系统的,对比第三方框架Snpkit)
        /// 转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //遮盖
        //
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //顶部
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        //房子
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息lable
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        //注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerButton , attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton , attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
    }
    
    
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 消息文字
    private lazy var messageLabel: UILabel = {
        let label: UILabel = UILabel()
        label.text = "关注一些人!看看有神马惊喜!"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        return label
    }()//闭包别忘了()号,否则报错
    
    //注册点击事件  (私有方法,是系统调用要加 @objc)
    @objc private func didClickregisterButton() {
    delegate?.visitorViewDidClickRegisterButton()
    }
    //登陆点击事件 (私有方法,是系统调用要加 @objc)
    @objc private func didClickloginButton() {
    delegate?.visitorViewDidClickLoginButton()
    }
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("didClickregisterButton"), forControlEvents: UIControlEvents.TouchUpInside)
    return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button: UIButton = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("didClickloginButton"), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
}



